package com.kfgj.hkp.common.interceptor;

import com.kfgj.hkp.common.context.CurrentContext;
import com.kfgj.hkp.common.context.RequestContext;
import com.kfgj.hkp.common.enums.ClientType;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description TODO
 * @auth liwensihan
 * @date 2018/6/28 18:40
 * @version 1.0
 * @return
 */
public class ClientFromInterceptor extends AbstractSpringInterceptor {

    @Override
    public boolean preInterceptor(HttpServletRequest request, HttpServletResponse response, Object handler){

        CurrentContext.clean();

        String userAgent = request.getHeader("user-agent");
        RequestContext context = CurrentContext.getRequestContext();
        context.setRequestIp(getIpAddress(request));
        if (userAgent!=null){
            String temp = userAgent.toLowerCase();
            if(temp.contains("android") || temp.contains("okhttp")){
                if(temp.contains("micromessenger")){
                    context.setClientType(ClientType.WeiXin_Android);
                }else if(temp.contains("okhttp")){
                    context.setClientType(ClientType.Android_APP);
                }else{
                    context.setClientType(ClientType.Android_Browser);
                }
            }else if(temp.contains("iphone")){
                if(temp.contains("micromessenger")){
                    context.setClientType(ClientType.WeiXin_Iphone);
                }else if(temp.contains("version/")){
                    context.setClientType(ClientType.Iphone_Browser);
                }else{
                    context.setClientType(ClientType.Iphone_APP);
                }
            }else{
                context.setClientType(ClientType.PC);
            }
        }else{
            context.setClientType(ClientType.PC);
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

    private String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
